/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package estg.dae.projecto.bank.data;

import estg.dae.projecto.bank.model.Account;
import estg.dae.projecto.bank.model.AccountMovement;
import java.util.Collection;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Marco
 */
@Stateless
public class AccountMovementDao implements AccountMovementDaoLocal {
    @PersistenceContext(unitName="Bank-ejb-PersistenceUnit")
    private EntityManager entityManager;

    public AccountMovement get(long id) {
        return entityManager.find(AccountMovement.class, id);
    }
    public void save(AccountMovement movement ){
        entityManager.persist(movement);
        entityManager.flush();
        entityManager.clear();
    }
    public void update(AccountMovement movement){
        entityManager.merge(movement);
        entityManager.flush();
        entityManager.clear();
    }
    public void delete(AccountMovement movement){
        entityManager.remove(movement);
        entityManager.flush();
        entityManager.clear();
    }
    public Collection<AccountMovement> getMovements(Account account, int movs){
        return (Collection<AccountMovement>) entityManager
                .createNativeQuery("SELECT m FROM AccountMovement m"// WHERE m.accountid = ?1"
                , AccountMovement.class)
                //.setParameter(1, account.getId())
                .setMaxResults(movs).getResultList();
    }

}
